/*!
 * MultiUpload for xheditor
 * @requires xhEditor
 *
 * @author Yanis.Wang<yanis.wang@gmail.com>
 * @site http://xheditor.com/
 * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
 *
 * @Version: 0.9.2 (build 100505)
 */
var swfu, selQueue = [];

function fileQueued(file) {
	
	//防止同名文件重复添加
	for (var i in selQueue) if (selQueue[i].name == file.name) {
		swfu.cancelUpload(file.id);
		layer.msg('这个文件已经上传过了,如确定需要再次上传,请先刷新页面', {icon: 2});
		return false;
	}

	selQueue.push(file);
}


function fileQueueError(file, errorCode, message) //队列添加失败
{
	var errorName = '';
	switch (errorCode) {
	case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
		errorName = "对不起，您最多可以上传 " + (this.settings.file_upload_limit) + " 个文件";
		break;
	case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
		errorName = "选择的图片超过了当前大小限制：" + this.settings.file_size_limit;
		break;
	case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		errorName = "零大小文件";
		break;
	case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		errorName = "文件扩展名必需为：" + this.settings.file_types_description + " (" + this.settings.file_types + ")";
		break;
	default:
		errorName = "未知错误";
		break;
	}
	layer.msg(errorName, {icon: 2});
}


function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

//单文件上传开始
function uploadStart(file) 
{
	layer.load();//正在加载
}

//单文件上传进度
function uploadProgress(file, bytesLoaded, bytesTotal) 
{
	
}

//单文件上传成功
function uploadSuccess(file, serverData) {
	
	layer.closeAll('loading');//关闭正在加载
	
	var data = Object;
	try {
		eval("data=" + serverData);
	} catch (ex) {};
	if (data.ret == 0) {
		//成功上传
		layer.msg(data.msg,{icon: 1}, function(){
			window.location.href = data.url;//跳转
		}); 
	} else {
		//上传失败
		layer.msg(data.msg, {icon: 2});
	}
	
	
}

//单文件上传错误
function uploadError(file, errorCode, message) 
{
	return message;
}

//文件上传周期结束
function uploadComplete(file) 
{
	if (swfu.getStats().files_queued > 0) swfu.startUpload();
	else uploadAllComplete();
}

function uploadAllComplete() {
	//layer.msg("文件上传完成", {icon: 1});
	layer.closeAll('loading');//关闭正在加载
}
